package pipeline;

import java.io.File;

import def.BioNLPDocument;

import setting.BioNLPSettings;
import util.io.FileUtil;
import exp.Count;

public class Parsing {
	public static void main(String[] args){
		BioNLPSettings.init(args[0]);
		BioNLPSettings.print();
		Count sum = new Count();
		String path  =BioNLPSettings.getValue("dataPath");
		//			BioNLPSettings.parsePath = BioNLPSettings.trainParsePath;
		for (String filename :  new File(path).list()){
			//						String filename = "9825820.txt";
			if (filename.endsWith(".txt")){
				//					if (r.nextDouble()> 0.1)
				//						continue;
				String filePrefix = filename.replace(".txt", "");
				System.out.println(filename+ " to be processed");
				sum.add(processDocument(path, filePrefix));
			}
		}
	}
	public static Count processDocument(String path, String filePrefix){
		Count c = new Count();
		BioNLPDocument doc = new BioNLPDocument(path, filePrefix);
		
		
		String parsePath = BioNLPSettings.getValue("parsePath");
		String targetPath = "data/BioNLP09/"+parsePath+"/pstree/";
		String targetDepPath = "data/BioNLP09/"+parsePath+"/dep/";
		String sourcePath = "data/BioNLP09/"+parsePath+"/sent/";
		if (!doc.valid){
			System.err.println("[FIX]"+"parser/biomodel/parse.sh "+sourcePath+filePrefix+" > "
					+"data/BioNLP09/"+parsePath+"/pstree/"+filePrefix+".pstree");
			System.err.println("[FIX]./generateDep.sh "+"data/BioNLP09/"+parsePath+"/pstree/"+filePrefix+".pstree "+"data/BioNLP09/"+parsePath+"/dep/"+filePrefix+".dep");
		}
		StringBuilder sb = new StringBuilder();
		if (doc.aText == null){
			System.err.println(""+filePrefix);
		}
		for (int i = 0; i < doc.aText.lines.length; i++){
			sb.append("<s> ");
			sb.append(doc.aText.lines[i]);
			sb.append(" </s>\n");
		}
		FileUtil.writeTextToFile(sb.toString(), sourcePath + filePrefix);
		System.out.println("echo "+filePrefix);
		System.out.println("~/software/charniakParser/biomodel/parse.sh "+sourcePath+filePrefix+" > "+targetPath+filePrefix+".pstree");
		System.out.println("./generateDep.sh "+targetPath+filePrefix+".pstree "+targetDepPath+filePrefix+".dep");
//		checkRegArg(doc);
//		checkInduction(doc);
//		checkPrep(doc);
		
		return c;
	}
}
